<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="老师" prop="createBy">
        <el-select v-model="queryParams.createBy" placeholder="请选择老师" clearable size="small">
          <el-option
            v-for="dict in thOptions"
            :key="dict.userId"
            :label="dict.nickName"
            :value="dict.nickName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

<!--      <el-table-column label="课程id" align="center" prop="courseId" />-->
      <el-table-column label="课程名称" align="center" prop="courseName" />
<!--      <el-table-column label="老师" align="center" prop="createBy" />-->
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已选人数" align="center" prop="number" />
      <el-table-column label="课程人数" align="center" prop="total" />
      <el-table-column label="未选人数" align="center" prop="remain" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-edit"
            :disabled="scope.row.status >= 2"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            type="text"
            icon="el-icon-view"
            @click="selectCourse(scope.row)"
          >查看名单</el-button>
          <el-button
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删 除</el-button>
          <el-button
            type="text"
            icon="el-icon-open"
            @click="handleOpen(scope.row)"
            :disabled="scope.row.status !== 1"
          >开 课</el-button>
          <el-button
            type="text"
            icon="el-icon-success"
            :disabled="scope.row.status !== 2"
            @click="handleEsc(scope.row)"
          >完 结</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程对话框 -->
    <!-- 添加或修改课程对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="课程人数" prop="total" v-if="this.title == '添加课程'">
          <el-input v-model="form.total" placeholder="请输入课程人数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <el-dialog :title="已选名单" :visible.sync="select" width="500px" append-to-body>
      <div class="preview-content">
        <!-- 图书信息 -->
        <div class="info-preview">
          <h1><strong>课程 : </strong> {{ form.courseName }} </h1>
          <p><strong>课程人数:</strong> ( {{ form.total  }} / {{ form.number  }} ) <strong>  剩余人数:</strong> {{ form.remain }}</p>
          <el-table  :data="student">
            <!--      <el-table-column label="课程id" align="center" prop="courseId" />-->
            <el-table-column label="姓名" align="center" prop="nickName" />
            <!--      <el-table-column label="老师" align="center" prop="createBy" />-->
            <el-table-column label="班级" align="center" prop="dept.deptName" />
            <el-table-column label="手机号" align="center" prop="phonenumber" />
            <el-table-column label="邮箱" align="center" prop="email" />
          </el-table>
        </div>
      </div>
      <!-- 底部按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="closePreview">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCourse2, getCourse, getStudent,delCourse, addCourse, updateCourse, exportCourse,th,selCourse } from "@/api/system/course/course";
import da from "element-ui/src/locale/lang/da";

export default {
  name: "create",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      select: false,
      // 状态字典
      statusOptions: [],
      // 教师字典
      thOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createTime: null,
        courseName: null,
        createBy: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单参数
      student: [],
      // 表单校验
      rules: {
        courseName: [
          { required: true, message: "课程名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTh();
    this.getDicts("course_status").then(response => {
      this.statusOptions = response.data;
      // console.info(this.statusOptions)
    });

  },
  methods: {
    /** 查询课程列表 */
    getList() {
      this.loading = true;
      listCourse2(this.queryParams).then(response => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    closePreview() {
      this.select = false;
      this.reset();
    },
    selectButton(){
      selCourse(this.form).then(response => {
        this.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
    },
    getTh() {
      this.loading = true;
      th().then(response => {
        this.thOptions = response.data;
        console.info(this.thOptions)
        this.loading = false;
      });
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        createTime: null,
        updateBy: null,
        courseId: null,
        courseName: null,
        number: null,
        delFlag: null,
        updateTime: null,
        createBy: null,
        total: null,
        remain: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.courseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const courseId = row.courseId || this.ids
      getCourse(courseId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程";
      });
    },
    /** 查看名单按钮操作 */
    selectCourse(row) {
      this.reset();
      const courseId = row.courseId || this.ids
      getCourse(courseId).then(response => {
        this.form = response.data;
      });
      getStudent(courseId).then(response => {
        this.student = response.data;
        this.select = true;
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.courseId != null) {
            updateCourse(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const courseIds = row.courseId || this.ids;
      this.$confirm('是否确认删除课程编号为"' + courseIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCourse(courseIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 开课按钮操作 */
    handleOpen(row) {
      const data = row
      data.status = 2
      this.$confirm('是否确认 【' + row.courseName + '】 开课?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return updateCourse(data);
      }).then(() => {
        this.getList();
        this.msgSuccess("操作成功");
      })
    },
    handleEsc(row) {
      const data = row
      data.status = 3
      this.$confirm('是否确认 【' + row.courseName + '】 完结?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return updateCourse(data);
      }).then(() => {
        this.getList();
        this.msgSuccess("操作成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有课程数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCourse(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
